// pages/my/applyRefund/applyRefund.js
import * as appConfig from "../../../app-config.js";
var app = getApp()
Page({
  data: {
    refundTypeM:"0rpx",
    refundTypeH:{},
    showReason:false,
    showtype:false,
    refundType:0,
    refundReason:0,
    isShow:true,
    imgs: [
      '../../../image/bg_imag.png',
      '../../../image/bg_imag.png',
      '../../../image/bg_imag.png',
    ],
    goodsList: [],
    blackPrice:true,
    goodsInfoShow:true,
    refundTypeInfo: [],
    refundReasonInfo: [],
    carNumber:'',
    person:'',
    telNumber:'',
    reason:'',
    num:1,
    applyPrice:0,
    orderId:'',
    orderNo:'',
    note:''
  },
  onLoad: function (options) {
    let productInfo = JSON.parse(options.info)
    console.log(productInfo)
    this.setData({
      ['goodsList[0]']:productInfo.productDetail,
      orderId:productInfo.orderId,
      orderNo:productInfo.orderNo,
      manufacturerId:productInfo.manufacturerId
    })
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
    })
    var refundReasonInfo= this.initRefundReason();
    var refundTypeInfo = this.initRefundWay();
    this.styleRefund();
    this.changePrice()
    this.setData({
      refundReasonInfo: refundReasonInfo,
      refundTypeInfo: refundTypeInfo,
      reason:refundReasonInfo[0].name
    })
  },
  getCarNumber(e){
    this.setData({
      carNumber:e.detail.value
    })
  },
  getPerson(e){
    this.setData({
      person:e.detail.value
    })
  },
  getTel(e){
    this.setData({
      telNumber:e.detail.value
    })
  },
  //上传凭证
  uploadImgTap:function(){
    var that=this;
    wx.chooseImage({
      count: 3,
      sourceType: ['album'],//本地相册
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths)
        let imgs = []
        for (var i = 0; i < tempFilePaths.length;i++){
          wx.uploadFile({
            url: appConfig.apiBase + '/wx_app/image/upload',
            filePath: tempFilePaths[i],
            name: 'image',
            success: function (res) {
              var str = res.data.split(',')
              let code = str[0].split(':')[1]
              if (code == 1) {
                let length = str[2].length - 3
                let url = str[2].substring(21,length)
                imgs.push(url)
                console.log(imgs)
                that.setData({
                  imgs
                })
              }else{
                app.errorMsg('上传图片失败')
              }
            }
          })
        }
      }
    })
  },
  //点击退回方式
  refundWayTap:function(){
    var refundType = this.data.refundType;
    var refundTypeInfo = this.initRefundWay();
    refundTypeInfo[refundType].selected = true;
    this.setData({
      showtype:true,
      refundTypeInfo: refundTypeInfo
    })
  },
  //初始化退回方式列表
  initRefundWay:function(){
   var  refundTypeInfo=[{
      type: 1,
      name: "厂家货运",
      selected: false
    }, {
      type: 2,
      name: "第三方物流",
      selected: false
    }]
    return refundTypeInfo
  },
  //选择退回方式
  choicerefundType:function(event){
    var index = event.currentTarget.dataset.index;
    var refundTypeInfo = this.initRefundWay();
    var selectInfo = refundTypeInfo[index];
    selectInfo.selected = true;
    this.setData({
      refundTypeInfo: refundTypeInfo
    })
  },
  //取消退回方式
  cancleTypeTap: function () {
    this.setData({
      showtype: false
    })
  },
  //确认退回方式
  sureTypeTap: function () {
    var refundTypeInfo = this.data.refundTypeInfo;
    for (var i = 0; i < refundTypeInfo.length; i++) {
      if (refundTypeInfo[i].selected == true) {
        this.setData({
          refundType: i,
          showtype: false
        })
        break;
      }
    }
    this.styleRefund();
  },
  styleRefund:function(){
    var refundType = this.data.refundType;
    if (refundType == 0) {
      this.setData({
        refundTypeM: "100rpx",
        refundTypeH: {

        },
      })
    } else {
      this.setData({
        refundTypeM: "",
        refundTypeH: {
          page: "100%",
          view: "calc(100% - 205rpx)"
        },
      })
    }
  },
  //点击退货原因
  redundReasontap: function () {
   var refundReasonInfo= this.initRefundReason()
    var refundReason=this.data.refundReason;
    refundReasonInfo[refundReason].selected=true;
    this.setData({
      refundReason: refundReason,
      showReason:true,
      refundReasonInfo: refundReasonInfo
    })
  },
  //初始化退货原因列表
  initRefundReason: function () {
    var refundReasonInfo = [{
      id: 1,
      name: "质量问题",
      selected: false
    }, {
      id: 2,
      name: "货物破损",
      selected: false
    }, {
      id: 3,
      name: "图片／产地／批号／规格描述不符",
      selected: false
    }, {
      id: 4,
      name: "卖家发错货",
      selected: false
    }, {
      id: 5,
      name: "三包",
      selected: false
    }, {
      id: 6,
      name: "其他（请在退货说明里详细描述）",
      selected: false
    }]
    return refundReasonInfo
  },
  //选择退货原因
  choicerefundReason:function(event){
    var index = event.currentTarget.dataset.index;
    var refundReasonInfo=this.initRefundReason();
    var selectInfo=refundReasonInfo[index];
    selectInfo.selected=true;
    this.setData({
      refundReasonInfo: refundReasonInfo
    })
  },
  //取消选择退货原因
  cancleReasonTap:function(){
    this.setData({
      showReason:false
    })
  },
  //确认选择退货原因
  sureReasonTap: function () {
    var refundReasonInfo = this.data.refundReasonInfo;
    for (var i = 0; i < refundReasonInfo.length;i++){
      if (refundReasonInfo[i].selected == true) {
        this.setData({
          refundReason:i,
          showReason: false,
          reason:refundReasonInfo[i].name
        })
        break;
      }
    }
  },
  add(){
    let num = this.data.num
    num++
    this.setData({
      num
    })
    this.changePrice()
  },
  sub(){
    let num = this.data.num
    if (num>0) {
      num--
    }
    this.setData({
      num
    })
    this.changePrice()
  },
  changeCount(e){
    this.setData({
      num:e.detail.value
    })
    this.changePrice()
  },
  //备注
  remark(e){
    this.setData({
      note:e.detail.value
    })
  },
  //价格
  changePrice(){
    this.setData({
      applyPrice:this.data.goodsList[0].price*this.data.num
    })
  },
  //提交申请
  applyRefund(){
    let data = {
      agentId:this.data.agentId,
      accountId:this.data.accountId,
      orderId:this.data.orderId,
      orderNo:this.data.orderNo,
      orderDetailId:this.data.goodsList[0].orderDetailId,
      logisticsType:this.data.refundType==0?1:2,
      returnsAmount:this.data.applyPrice,
      amount:this.data.num,
      why:this.data.reason,
      note:this.data.note,
      imageUrls:Object.keys(this.data.imgs).length==0?'':this.data.imgs,
      carNo:this.data.carNumber,
      driver:this.data.person,
      driverPhone:this.data.telNumber,
      manufacturerId:this.data.manufacturerId
    }
    console.log(data)
    app.request('wx_app/after/sale/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let id = res.data.applyId      
        wx.navigateTo({
          url:'../refundDetail/refundDetail?refundStatus=1&returnsId='+id
        })
      }
    })
  }
})